#include "ScenceControl.h"
#include "ui_ScenceControl.h"

#include <iostream>
#include <fstream>

ScenceControl::ScenceControl(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::ScenceControl)
{
    ui->setupUi(this);
    model = new QStringListModel;
    ui->cb_path->setModel(model);
    initSignals();
}

ScenceControl::~ScenceControl()
{
    delete ui;
}

void ScenceControl::initSignals()
{
    connect(ui->hs_camx,&QAbstractSlider::sliderMoved ,this,[=](int f){
        emit CommitConfig(TP_CAMX,f);
        ui->hs_camx->setValue(0);
    });
    connect(ui->hs_camy,&QAbstractSlider::sliderMoved ,this,[=](int f){
        emit CommitConfig(TP_CAMY,f);
        ui->hs_camy->setValue(0);
    });
    connect(ui->hs_camz,&QAbstractSlider::sliderMoved ,this,[=](int f){
        emit CommitConfig(TP_CAMZ,f);
        ui->hs_camz->setValue(0);
    });
    connect(ui->hs_Scale,&QAbstractSlider::sliderMoved ,this,[=](int f){
        emit CommitConfig(TP_CAMS,f);
    });
    connect(ui->cb_Cam, QOverload<int>::of(&QComboBox::activated),[=](int index){
        emit CommitConfig(TP_CAM_CHANGED,index);
    });
}

void ScenceControl::on_cb_Sele_activated(int index)
{
    sys::SysConf::GetInstance()->curModel = static_cast<sys::SelectModel>(index) ;
}

void ScenceControl::on_pb_upd_clicked()
{
    QDir dir("rundata/");
    QStringList nameFilters;
    nameFilters << "*.txt" << "*.path";
    QStringList files = dir.entryList(nameFilters, QDir::Files|QDir::Readable, QDir::Name);
    model->setStringList(files);
    ui->cb_path->setCurrentIndex(0);
}

void ScenceControl::on_pb_draw_clicked()
{
    QString fileName = "rundata/";
    fileName += ui->cb_path->currentText();
    std::ifstream infile(fileName.toStdString());
    std::string line;
    std::vector<Pos3dzx> f;
    while (std::getline(infile, line)){
        f.push_back( Weldroid::RobotPos::instance(line));
    }
    emit CommitPaths(f);
}

void ScenceControl::on_hz_zoom_actionTriggered(int action)
{
    emit CommitConfig(TP_ZOOM,action);
}

void ScenceControl::on_sp_xStep_valueChanged(int arg1)
{
    emit CommitConfig(TP_X_STEP,arg1);
}

void ScenceControl::on_sp_yStep_valueChanged(int arg1)
{
    emit CommitConfig(TP_Y_STEP,arg1);
}

void ScenceControl::on_cb_grid_toggled(bool checked)
{
    emit CommitConfig(TP_GRID,checked ? 1:0);
}

void ScenceControl::on_le_cam_editingFinished()
{
    std::string val = ui->le_cam->text().toStdString();
    Pos3dzx f = Pos3dzx::instance(val);
    emit CommitCamMove(f);
}
